﻿var ActivityHelper = function () {
    this.Activitys = new Array();
}

ActivityHelper.prototype = {
    StartActivity: null,
    EndActivity: null,
    DraftingActivity: null,
    Activitys: [],

    //添加节点
    /*
    e：当前位置信息
    activityType：节点类型
    isload：调用此方法的位置
        1、从画布上手动绘制的
        2、打开或者导入流程时
        默认值 undefined
        当手动绘制时，添加规则操作会记录操作历史。
    */
    AddActivity: function (point, activityType, isload) {
        var me = this;
        // 1、移除窗体上所有空间的样式（被选中）
        canvasHelper
        canvasHelper.ClearAllStyle();
        var canInfo = canvasHelper.InitialCanvas();
        //2、增加节点
        //var point = canvasHelper.GetPoint(e);
        var activity = new Activity(activityType, point, canInfo, isload);
        if (isload == undefined) isload = false;
        if (activityType == ActivityType.Branching && !isload) {
            canInfo = canvasHelper.InitialCanvas();
            new Activity(ActivityType.Merging, new Point(point.X + 350, point.Y), canInfo);
        }
        if (!isDoubleClickCommand)
            gcommand = Command.NullOperation;
        if (!isload)
            ToolBar.HistroyOpertion();
        return activity;
    },


    //获取所有节点的可退回节点
    /*
    此方法为递归方法，初始值为me.DraftingActivity,依此后推
    当节点没有到DraftingActivity的路径时，无法计算出前驱节点
    */
    GetReturnActivitys: function (activity) {
        var me = this;
        activity.LoopCount += 1;

        if (activity.activityType == ActivityType.Drafting) {
            $.each(me.Activitys, function (i, val) { val.Preactivity = new Array(); });
        }


        $.each(activity.FromRule, function (i, rule) {
            var ac = rule.endActivity;

            var isContainsNew = false

            if (activity.Preactivity == undefined)
                activity.Preactivity = new Array();
            if (ac.Preactivity.length == 0) {
                isContainsNew = true;
                $.each(activity.Preactivity, function (j, preac) {
                    ac.Preactivity.push( preac);
                 
                });
            }
            else {
                $.each(activity.Preactivity, function (j, preac) {
                    if (!ac.Preactivity.where(preac.guid) && ac.guid != preac.guid) {
                        ac.Preactivity.push( preac);
                        isContainsNew = true;
                    }
                });
            }
            if (ac.Preactivity.where(activity.guid) == undefined) {
                ac.Preactivity.push( activity);
                isContainsNew = true;
            }

            if (isContainsNew)
                me.GetReturnActivitys(rule.endActivity);
        });
    },

    //释放所有的对象
    /*一般用于全新创建流程*/
    Dispose: function () {
        var me = this;
        for (i = me.Activitys.length - 1; i >= 0; --i) {
            if (me.Activitys[i] != undefined)
                me.Activitys[i].Dispose(true);
        }
    }

}